package com.lzw.cpvideoplayer.adapter;

import android.graphics.Color;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.viewpager.widget.PagerAdapter;

/**
 * 主页轮播图适配器
 */
public class HomePagerAdapter extends PagerAdapter {
    private List<Integer> mList;
    private int nPosition;
    public HomePagerAdapter() {
        mList = new ArrayList<>();
    }

    /**
     * 初始化
     * @param container
     * @param position
     * @return
     */
    @NonNull
    @Override
    public Object instantiateItem(@NonNull ViewGroup container, int position) {
        //初始化
         nPosition = position % mList.size();
        ImageView imageView = new ImageView(container.getContext());
        imageView.setImageResource(mList.get(nPosition));
        imageView.setScaleType(ImageView.ScaleType.FIT_XY);
        imageView.setBackgroundColor(Color.GRAY);
    //设置完颜色就添加到容器里
        container.addView(imageView);

        return imageView;
    }

    /**
     * 销毁
     * @param container
     * @param position
     * @param object
     */
    @Override
    public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
        container.removeView((View) object);

    }

    /**
     * 获取长度
     * @return
     */
    @Override
    public int getCount() {
        if (mList != null) {
            return Integer.MAX_VALUE;//无限个
        }
        return 0;
    }

    @Override
    public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
        return view == object;
    }

    public void setData(List<Integer> list) {
        //给颜色容器赋值

        mList = list;

    }
    public int getPicSizeCount(){

        if (mList != null) {
            return mList.size();
        }
        return 0;
    }
}

